<template>
    <section class="chart-container">
        <el-row>
            <el-col :span="12">
                <div id="chartColumn" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartBar" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartLine" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartPie" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="24">
                <a href="http://echarts.baidu.com/examples.html" target="_blank" style="float: right;">more>></a>
            </el-col>
        </el-row>
    </section>
</template>

<script>
    import echarts from 'echarts'

    export default {
        data() {
            return {
                chartColumn: null,
                chartBar: null,
                chartLine: null,
                chartPie: null
            }
        },
        methods: {
            drawColumnChart() {
              //直接在这里发送请求
              this.$http.get("/echarts/shopRegisterCount").then(res=>{
                 if(res.data.success) {

                      this.chartColumn = echarts.init(document.getElementById('chartColumn'));
                      this.chartColumn.setOption({
                        title: {text: 'Column Chart'},
                        tooltip: {},
                        xAxis: {
                          data: res.data.data.name
                        },
                        yAxis: {},
                        series: [{
                          name: '销量',
                          type: 'bar',
                          data: res.data.data.num
                        }]
                      });
                 }
                });
            },
            drawBarChart() {
              //直接在这里发送请求
              this.$http.get("/echarts/shopRegisterCount").then(res=> {
                if (res.data.success) {
                  this.chartBar = echarts.init(document.getElementById('chartBar'));
                  this.chartBar.setOption({
                    title: {
                      text: 'Bar Chart',
                      subtext: '数据来自网络'
                    },
                    tooltip: {
                      trigger: 'axis',
                      axisPointer: {
                        type: 'shadow'
                      }
                    },
                    legend: {
                      data: ['2011年', '2012年']
                    },
                    grid: {
                      left: '3%',
                      right: '4%',
                      bottom: '3%',
                      containLabel: true
                    },
                    xAxis: {
                      type: 'value',
                      boundaryGap: [0, 0.01]
                    },
                    yAxis: {
                      type: 'category',
                      data: res.data.data.name
                    },
                    series: [
                      {
                        name: '2011年',
                        type: 'bar',
                        data: res.data.data.num
                      },
                      {
                        name: '2012年',
                        type: 'bar',
                        data: [1, 2, 3, 12]
                      }
                    ]
                  });
                }
              });
            },
            drawLineChart() {
              //直接在这里发送请求
              this.$http.get("/echarts/shopRegisterCount").then(res=>{
                if(res.data.success){
                  // var num = res.data.data.map(item => item.num);
                  // var name = res.data.data.map(item => item.name);
                  // console.log(num)
                  // console.log(name)
                  this.chartLine = echarts.init(document.getElementById('chartLine'));
                  this.chartLine.setOption({
                  title: {
                    text: '注册店铺的数量'
                  },
                  tooltip: {
                    trigger: 'axis'
                  },
                  legend: {
                    data: ['店铺注册数量']
                  },
                  grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                  },
                  xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: res.data.data.name
                  },
                  yAxis: {
                    type: 'value'
                  },
                  series: [
                    {
                      name: '店铺注册数量',
                      type: 'line',
                      stack: '总量',
                      data: res.data.data.num
                    }
                  ]
                });
                }
              })

            },
            drawPieChart() {
              //直接在这里发送请求
              this.$http.get("/echarts/shopRegisterCount").then(res=> {
                if (res.data.success) {
                  console.log(res.data)
                  this.chartPie = echarts.init(document.getElementById('chartPie'));
                  this.chartPie.setOption({
                    title: {
                      text: 'Pie Chart',
                      subtext: '纯属虚构',
                      x: 'center'
                    },
                    tooltip: {
                      trigger: 'item',
                      formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                      orient: 'vertical',
                      left: 'left',
                      data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
                    },
                    series: [
                      {
                        name: '访问来源',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '60%'],
                        data:res.data.data.data,
                        itemStyle: {
                          emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                          }
                        }
                      }
                    ]
                  });
                }
              });
            },
            drawCharts() {
                this.drawColumnChart()
                this.drawBarChart()
                this.drawLineChart()
                this.drawPieChart()
            },
        },

        mounted: function () {
          //初始化
          this.drawCharts()
        },
        updated: function () {
            this.drawCharts()
        }
    }
</script>

<style scoped>
    .chart-container {
        width: 100%;
        float: left;
    }
    /*.chart div {
        height: 400px;
        float: left;
    }*/

    .el-col {
        padding: 30px 20px;
    }
</style>
